﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;

using System.Collections.ObjectModel;
using PunchCard.Service;
using PunchCard.Model;
using System.IO;
using static PunchCard.Model.ModelMainWindow;

namespace PunchCard.View
{
    /// <summary>
    /// PunchCard.xaml 的交互逻辑
    /// </summary>
    public partial class PunchCardInterface : UserControl
    {
        public PunchCardInterface()
        {
            InitializeComponent();
            DataContext = ViewModel.ViewModel.GetInstance().m_Model_MainWindow;
        }

        private ModelMainWindow MODELFUNC = ViewModel.ViewModel.GetInstance().m_Model_MainWindow;

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            IDnum.Text = CsINI.ReadRealValue("card.ini", "OA", "工号");
            Reason.Text = CsINI.ReadRealValue("card.ini", "OA", "原因");
        }

        private void btn_OpenFilePath_Click(object sender, RoutedEventArgs e)
        {
            string strFilePath = string.Empty;
            if (CsGetFileDlg.Win32_GetFilePath(ref strFilePath))
            {
                MODELFUNC.strFilePath = strFilePath;
                if (MODELFUNC.m_nModel == 0 || MODELFUNC.m_bBatchModel)
                {
                    if (MODELFUNC.strFilePath == string.Empty || MODELFUNC.strFilePath == null)
                        return;
                    MODELFUNC.LookupLists(MODELFUNC.strFilePath, IDnum.Text, Reason.Text);
                }
            }
        }

        private void btn_Lookup_Click(object sender, RoutedEventArgs e)
        {
            if (MODELFUNC.strFilePath == string.Empty || MODELFUNC.strFilePath == null)
                return;
            MODELFUNC.LookupLists(MODELFUNC.strFilePath, IDnum.Text, Reason.Text);
            CsINI.WriteIniValue("card.ini", "OA", "工号", IDnum.Text);
            CsINI.WriteIniValue("card.ini", "OA", "原因", Reason.Text);
        }

        private void btn_CreateNewExcel_Click(object sender, RoutedEventArgs e)
        {
            string strFilePath = string.Empty;
            if (CsGetFileDlg.Win32_WriteNewFilePath(ref strFilePath))
                CsExcelWrite.ExportLists(strFilePath, MODELFUNC.m_LExcel);
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            string Filepath = string.Empty;
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                Filepath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (Filepath != string.Empty)
            {
                MODELFUNC.LookupLists(Filepath, IDnum.Text, Reason.Text);
                CsINI.WriteIniValue("card.ini", "OA", "工号", IDnum.Text);
                CsINI.WriteIniValue("card.ini", "OA", "原因", Reason.Text);
            }
        }

        private void CheckBox_Model_Click(object sender, RoutedEventArgs e)
        {
            RadioButton bt = sender as RadioButton;
            if (C1 != null && C2 != null && C3 != null)
            {
                string strName = bt.Name.Substring(1, 1);
                switch (strName)
                {
                    case "1":
                        MODELFUNC.m_nModel = 0;
                        IDnum.Visibility = Visibility.Hidden;
                        InputTextDlg login1 = new InputTextDlg();
                        login1.ShowDialog();
                        if (login1.DialogResult == true)
                            MODELFUNC.GetWorkingDays(MODELFUNC.m_nYear, MODELFUNC.m_nMonth);
                        else
                        {
                            C1.IsChecked = false;
                            C2.IsChecked = true;
                        }
                        break;
                    case "2":
                        MODELFUNC.m_nModel = 1;
                        if (C4.IsChecked == false)
                        {
                            IDnum.Visibility = Visibility.Visible;
                            CtlBatch.Visibility = Visibility.Visible;
                        }
                        break;
                    case "3":
                        MODELFUNC.m_nModel = 2;
                        if (MessageBox.Show("本月是否都在外出差", "模式选择", MessageBoxButton.YesNo,
                            MessageBoxImage.Information) != MessageBoxResult.Yes)
                        {
                            C2.IsChecked = true;
                            break;
                        }
                        InputTextDlg login2 = new InputTextDlg();
                        login2.ShowDialog();
                        if (login2.DialogResult != true)
                        {
                            C2.IsChecked = true;
                            break;
                        }
                        MODELFUNC.GetWorkingDays(MODELFUNC.m_nYear, MODELFUNC.m_nMonth);
                        FileParam param = new FileParam()
                        {
                            FileName = "",
                            FileID = "",
                            Reason = Reason.Text,
                        };
                        MODELFUNC.Model2(ref param);
                        IDnum.Visibility = Visibility.Hidden;
                        C3.IsChecked = false;
                        break;
                }
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            string strSrcFile = AppDomain.CurrentDomain.BaseDirectory + "批量导出人员名单.xls";
            if (C4.IsChecked == false)
                CtlBatch.Visibility = Visibility.Visible;
            else
            {
                if (System.IO.File.Exists(strSrcFile))
                {
                    if (!MODELFUNC.ex_Read_3(strSrcFile))
                        C4.IsChecked = false;
                    else
                    {
                        IDnum.Visibility = Visibility.Hidden;
                        MODELFUNC.m_bBatchModel = true;
                        CtlBatch.Visibility = Visibility.Hidden;
                    }
                }
                else
                {
                    Console.WriteLine("不存在该文件");
                    CsExcelWrite.WriteListModelFile(strSrcFile);
                    C4.IsChecked = false;
                }
            }
        }
    }
}
